﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VRB_0._1
{
    class TestMap : Map
    {
     
        TimeSpan SpawnRate;
        TimeSpan SpawnRate2;
        TimeSpan LastSpawn;
        TimeSpan LastSpawn2;

        GaffaSpawner gs;

        public override void Initialize(ContentManager Content, Viewport Viewport)
        {
            //denne linja må være med, men kun som copy/paste.
            base.Initialize(Content, Viewport);
            SpawnRate= TimeSpan.FromSeconds(1.3f);
            
            SpawnRate2 = TimeSpan.FromSeconds(0.6f);

            LastSpawn = TimeSpan.Zero;
            LastSpawn2 = TimeSpan.Zero;
            
            //background = Content.Load<Texture2D>("");
            winRect = new Rectangle(1500, 300, 40, 40);
            
            //Her defineres startposisjonen til spilleren
            avatarStartPosition = new Vector2(Viewport.TitleSafeArea.X,
                Viewport.TitleSafeArea.Y + Viewport.TitleSafeArea.Height / 2);

            //Her lastes inn all tekstur som skal brukes
            Texture2D packagingBoxTexture = Content.Load<Texture2D>("cardboardboxlowres");
            Texture2D groundPlainTexture = Content.Load<Texture2D>("ground_1_Blank");
            Texture2D groundBoneTexture = Content.Load<Texture2D>("ground_1_Bone");
            Texture2D groundSkullTexture = Content.Load<Texture2D>("ground_1_Skull");
            

            SpawnPlaces.Add(480.0f);
            SpawnPlaces.Add(600.0f);

            Button b1 = new Button();
            b1.Initialize(Viewport, Content, new Vector2(530.0f, 301.0f),Button.State.OFF);
            objects.Add(b1);

            gs = new GaffaSpawner();
            gs.Initialize(Viewport, CT, new Vector2(481.0f, 317.0f), GaffaSpawner.State.ON);
            objects.Add(gs);

            GroundPlain gp1 = new GroundPlain();
            gp1.Initialize(Viewport, groundPlainTexture, new Vector2(20.0f, 500.0f));
            objects.Add(gp1);
            GroundBone gb1 = new GroundBone();
            gb1.Initialize(Viewport, groundBoneTexture, new Vector2(420.0f, 500.0f));
            objects.Add(gb1);
            GroundSkull gs1 = new GroundSkull();
            gs1.Initialize(Viewport, groundSkullTexture, new Vector2(620.0f, 500.0f));
            objects.Add(gs1);
            GroundPlain gp2 = new GroundPlain();
            gp2.Initialize(Viewport, groundPlainTexture, new Vector2(820.0f, 500.0f));
            objects.Add(gp2);
            GroundPlain gp3 = new GroundPlain();
            gp3.Initialize(Viewport, groundPlainTexture, new Vector2(920.0f, 500.0f));
            objects.Add(gp3);
            GroundPlain gp4 = new GroundPlain();
            gp4.Initialize(Viewport, groundPlainTexture, new Vector2(1420.0f, 500.0f));
            objects.Add(gp4);

            PackagingBox pb5 = new PackagingBox();
            pb5.Initialize(Viewport, packagingBoxTexture, new Vector2(1242.0f, 365.0f));
            objects.Add(pb5);

            background = Content.Load<Texture2D>("bakgrunnsforslag1");

            base.InitializeAfterChild();
            this.initializeEnemies();
        }
        private void initializeEnemies()
        {
            DuctTapeEnemy dte2 = new DuctTapeEnemy();
            dte2.Initialize(VP, CT, new Vector2(320, 335.0f), DuctTapeEnemy.Direction.LEFT);
            enemies.Add(dte2);
           
        }
        public override void Update(GameTime gameTime)
        {
            if (SpawnEnemies)
            {
                if ((gameTime.TotalGameTime - LastSpawn) > SpawnRate)
                {
                    DuctTapeEnemy dte = new DuctTapeEnemy();
                    dte.Initialize(VP, CT, new Vector2(SpawnPlaces[0], 320.0f), DuctTapeEnemy.Direction.LEFT);
                    enemies.Add(dte);

                    LastSpawn = gameTime.TotalGameTime;

                }
                if ((gameTime.TotalGameTime - LastSpawn2) > SpawnRate2)
                {
                    DuctTapeEnemy dte2 = new DuctTapeEnemy();
                    dte2.Initialize(VP, CT, new Vector2(SpawnPlaces[1], 320.0f), DuctTapeEnemy.Direction.RIGHT);
                    enemies.Add(dte2);
                    LastSpawn2 = gameTime.TotalGameTime;
                }
            }

            foreach (DuctTapeEnemy enemy in enemies)
            {
                if (enemy.Active)
                    enemy.Update(gameTime);
            }
            if (ButtonON)
            {
                if (gameTime.TotalGameTime - TimeButtonNotPressed < TimeButtonCanBePressed)
                {
                    SpawnEnemies = false;
                    gs.currentState = GaffaSpawner.State.OFF;
                }
                else
                {
                    SpawnEnemies = true;
                    gs.currentState = GaffaSpawner.State.ON;
                }
            }
            else
            {
                TimeButtonNotPressed = gameTime.TotalGameTime;
                SpawnEnemies = true;
                gs.currentState = GaffaSpawner.State.ON;
            }
            foreach (VisualObject dudd in objects)
            {
                dudd.Update(gameTime);
            }
            
        }
        public override void UpdateMap(float movespeed, bool forward)
        {
            base.UpdateMap(movespeed, forward);
        }

        public override Game1.ENDING IsEnd(Avatar avatar, Viewport Viewport)
        {
            //Her testes det på om spilleren berører mål eller ikke. Bytt ut posisjoner i rect2 med deres målområde.
            //Foreløpig kan man kun dø om man faller nedenfor skjermen. Dette skal jeg endre etter hvert.
            Rectangle rect1 = new Rectangle((int)avatar.Position.X, (int)avatar.Position.Y, avatar.Width, avatar.Height);
            Rectangle rect2 = new Rectangle(760, 115, 50, 10);
            if (rect1.Intersects(winRect))
                return Game1.ENDING.WIN;
            else if (avatar.Position.Y >= Viewport.Height)
                return Game1.ENDING.LOSE;
            else
                return Game1.ENDING.NOTYET;
        }
        public override void Restart(Avatar avatar)
        {
            LastSpawn = LastSpawn - TimeSpan.FromSeconds(1.3f);
            base.Restart(avatar);
            this.initializeEnemies();
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }
    }
}
